<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="合同文件名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户合同文件名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="ID" width="100" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="客户合同文件名"
        align="center"
        prop="contractFileName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="客户身份" align="center" prop="docContent" />
      <el-table-column label="状态" align="center" prop="contractStatus">
        <template slot-scope="scope">
          <el-tag :type="scope.row.contractStatus == 2 ? 'success' : ''">{{
            scope.row.contractStatus == 2 ? "完成" : "未完成"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.rows"
      @pagination="getList"
    />

    <!-- 添加或修改定时任务对话框 -->
    <el-dialog title="详情" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="客户合同文件名">
              <span>{{ form.contractFileName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          :disabled="!Boolean(form.contractFileName)"
          @click="submitForm(0)"
          >下载AI修正文件</el-button
        >
        <el-button
          type="primary"
          :disabled="!Boolean(form.contractFileName)"
          @click="submitForm(1)"
          >下载AI分析结果</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { contractPage } from "@/api/fzz";

export default {
  name: "Job",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 是否显示Cron表达式弹出层
      openCron: false,
      // 传入的表达式
      expression: "",
      // 查询参数
      queryParams: {
        page: 1,
        rows: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jobName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      contractPage(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    onOpen() {
      const riskFileUrl = JSON.parse(this.form.riskFileUrl);
      console.log(riskFileUrl);
      // window.open(
      //   encodeURI(
      //     `https://pro.nebulabuild.top/aiapi/download_contract?filename=${this.form.contractFileUrl}`
      //   )
      // );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.open = true;
    },
    submitForm(type) {
      const riskFileUrl = JSON.parse(this.form.riskFileUrl);
      window.open(
        `https://wxfzz.zhixinglegalaid.cn/aiapi/download_contract?filename=${
          type ? riskFileUrl.analysis : riskFileUrl.revise
        }`
      );
      // this.open = false;
      // console.log(riskFileUrl);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobIds = row.jobId || this.ids;
      this.$modal
        .confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项？')
        .then(function () {
          return delJob(jobIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.file {
  color: #1890ff;
  cursor: pointer;
}
</style>